//
//  PayMoneyRecodeViewController.m
//  PropertyManager
//
//  Created by TNG on 2018/7/12.
//  Copyright © 2018年 Tng. All rights reserved.
//

#import "PayMoneyRecodeViewController.h"
#import "NotPayModel.h"
#import "SWNotPayTableViewCell.h"
#import "SWNotPayDetailsViewController.h"
@interface PayMoneyRecodeViewController () <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (strong, nonatomic) UITableView * tableview;
@property (nonatomic, strong) UITextField * textFiled;
@property (strong, nonatomic) UIView * bgView;
@property (nonatomic,assign) BOOL isHeadFresh;
@property (nonatomic,assign) int page;
@property (strong, nonatomic) NSMutableArray * dataArr;

@property (nonatomic, strong) UIButton * reSetBtn;
@property (nonatomic, strong) UIButton * searchBtn;
@property (nonatomic, strong) UIButton * startDataBtn;
@property (nonatomic, strong) UIButton * endDataBtn;
@property (nonatomic, strong) UILabel  * detailLabel;

@property (nonatomic,copy) NSString * startTime;
@property (nonatomic,copy) NSString * endTime;

@property (strong, nonatomic) NotPayModel * moneyModel;

@end

@implementation PayMoneyRecodeViewController

//static NSString * PayMoneyTableViewCellID = @"PayMoneyTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    self.startTime = @"";
    self.endTime = @"";
    self.title = @"缴费记录";
   
    [self.view addSubview:self.startDataBtn];
    [self.view addSubview:self.detailLabel];
    [self.view addSubview:self.endDataBtn];
    [self.view addSubview:self.reSetBtn];
    [self.view addSubview:self.searchBtn];
    [self creatTableview];
    [self registCell];
    [self RequestData];
}

-(UIButton *)startDataBtn
{
    if (_startDataBtn == nil) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH, WIDTH/2 - 70, 44)];
        btn.titleLabel.font = font(14);
        [btn setTitle:@"开始年月" forState:UIControlStateNormal];
        [btn setTitleColor:SFM153 forState:UIControlStateNormal];;
        [btn addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _startDataBtn = btn;
    }
    return _startDataBtn;
}

#pragma mark - 点击开始日期
-(void)startBtnClick:(UIButton *)btn
{
    WeakSelf;
    [[YearMonthDataPickview shareView] showView];
    [YearMonthDataPickview shareView].sendYearMonthDayBlcok = ^(NSString * _Nonnull year, NSString * _Nonnull month) {
        weakSelf.startTime = [NSString stringWithFormat:@"%@-%@",year,month];
        [weakSelf.startDataBtn setTitle:[NSString stringWithFormat:@"%@年%@月",year,month] forState:UIControlStateNormal];
        [weakSelf.startDataBtn setTitleColor:SFM51 forState:UIControlStateNormal];
    };
}

-(UILabel *)detailLabel
{
    if (_detailLabel == nil) {
        _detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.startDataBtn.frame), kStatusAndNavBarH, 20, 44)];
        _detailLabel.text = @"至";
        _detailLabel.font = font(14);
        _detailLabel.textColor = [UIColor redColor];
        _detailLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _detailLabel;
}

-(UIButton *)endDataBtn
{
    if (_endDataBtn == nil) {
        _endDataBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.detailLabel.frame), kStatusAndNavBarH, WIDTH/2 - 70, 44)];
        _endDataBtn.titleLabel.font = font(14);
        [_endDataBtn setTitle:@"结束年月" forState:UIControlStateNormal];
        [_endDataBtn setTitleColor:SFM153 forState:UIControlStateNormal];;
        [_endDataBtn addTarget:self action:@selector(endBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _endDataBtn;
}

#pragma mark - 结束年月
-(void)endBtnClick:(UIButton *)btn
{
    WeakSelf;
    [[YearMonthDataPickview shareView] showView];
    [YearMonthDataPickview shareView].sendYearMonthDayBlcok = ^(NSString * _Nonnull year, NSString * _Nonnull month) {
        weakSelf.endTime = [NSString stringWithFormat:@"%@-%@",year,month];
        [weakSelf.endDataBtn setTitle:[NSString stringWithFormat:@"%@年%@月",year,month] forState:UIControlStateNormal];
        [weakSelf.endDataBtn setTitleColor:SFM51 forState:UIControlStateNormal];
    };
}

-(UIButton *)reSetBtn
{
    if (_reSetBtn == nil) {
        _reSetBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.endDataBtn.frame), kStatusAndNavBarH, 60, 44)];
        _reSetBtn.titleLabel.font = font(14);
        [_reSetBtn setTitle:@"重置" forState:UIControlStateNormal];
        [_reSetBtn setTitleColor:SFM153 forState:UIControlStateNormal];;
        [_reSetBtn addTarget:self action:@selector(reSetBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reSetBtn;
}

#pragma mark -  重置
-(void)reSetBtnClick:(UIButton *)btn
{
    self.startTime = @"";
    self.endTime = @"";
    [self.startDataBtn setTitle:@"开始年月" forState:UIControlStateNormal];
    [self.endDataBtn setTitle:@"结束年月" forState:UIControlStateNormal];
    self.isHeadFresh = YES;
    self.page = 1;
    [self RequestData];
}

-(UIButton *)searchBtn
{
    if (_searchBtn == nil) {
        _searchBtn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.reSetBtn.frame), kStatusAndNavBarH, 60, 44)];
        _searchBtn.titleLabel.font = font(14);
        [_searchBtn setTitle:@"搜素" forState:UIControlStateNormal];
        [_searchBtn setTitleColor:SFMBlueColor forState:UIControlStateNormal];;
        [_searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

#pragma mark - 搜索
-(void)searchBtnClick:(UIButton *)btn
{
    self.isHeadFresh = YES;
    self.page = 1;
    [self RequestData];
}

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark - Data
-(void)RequestData
{
    WeakSelf;
//    NSString * urlStr = [NSString stringWithFormat:@"rest/gbJfjljfpController/getList?pageno=%d&pagesize=10&pkCustomerid=%@&startTime=%@&endTime=%@",self.page,SFMgbuserid,self.startTime,self.endTime];
    NSString * urlStr = [NSString stringWithFormat:@"/rest/gbJfjljfpController/getCount?pageno=%d&pagesize=10&pkCustomerid=%@&startTime=%@&endTime=%@",self.page,SFMgbuserid,self.startTime,self.endTime];
    [[HTTPClient shareClient] requestWithPath:urlStr AndReturnBlock:^(NSDictionary *dic) {
        NSArray * dataArr = dic[@"data"];
        for (NSDictionary * dics in dataArr) {
            NotPayModel * model = [NotPayModel yy_modelWithDictionary:dics];
            [weakSelf.dataArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } AndFailMessage:^(NSString *message, NSString *errorCode) {
        [SVProgressHUD ShowErrorMessage:message];
    }];
}

-(void)setNavBar{
    [self.bgView addSubview:self.textFiled];
    self.navigationItem.titleView = self.bgView;
}

-(UIView *)bgView
{
    if (_bgView == nil) {
        _bgView =[[UIView alloc]initWithFrame:CGRectMake(0, 7, WIDTH-100, 30)];
        _bgView.userInteractionEnabled=YES;
        _bgView.layer.cornerRadius=2;
        _bgView.clipsToBounds=YES;
        _bgView.backgroundColor = SFM242;
    }
    return _bgView;
}

-(UITextField *)textFiled
{
    if (_textFiled == nil) {
        _textFiled = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 100 , 30)];
        _textFiled.placeholder=@"请输入缴费记录相关信息";
        _textFiled.delegate =self;
        _textFiled.font=font(15);
        _textFiled.returnKeyType=UIReturnKeySearch;
    }
    return _textFiled;
}

-(void)registCell
{
     [self.tableview registerNib:[UINib nibWithNibName:@"SWNotPayTableViewCell" bundle:nil] forCellReuseIdentifier:@"SWNotPayTableViewCell"];
}

-(void)creatTableview{
    if (self.tableview == nil) {
        UITableView * tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusAndNavBarH + 44, WIDTH, HEIGHT - kStatusAndNavBarH - kHomeLineH - 44) style:UITableViewStylePlain];
        tableview.delegate = self;
        tableview.dataSource = self;
        if (@available(iOS 11.0, *)) {
            tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
        }
        self.tableview = tableview;
        [self.view addSubview:self.tableview];
    }
    // 下拉刷新
    WeakSelf;
    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.isHeadFresh = YES;
        weakSelf.page = 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_header endRefreshing];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    self.tableview.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 结束刷新
        weakSelf.isHeadFresh = NO;
        weakSelf.page += 1;
        [weakSelf RequestData];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SWNotPayTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SWNotPayTableViewCell" forIndexPath:indexPath];
    self.moneyModel = self.dataArr[indexPath.row];
    cell.historyModel = self.moneyModel;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.moneyModel = self.dataArr[indexPath.row];
    SWNotPayDetailsViewController * vc = [[SWNotPayDetailsViewController alloc] init];
    vc.model = self.moneyModel;
    [self.navigationController pushViewController:vc animated:YES];
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    PayMoneyLatestDetailViewController * ctr = [[PayMoneyLatestDetailViewController alloc] init];
//    ctr.isPay = NO;
//    HistoryPayModel * model = [self.dataArr objectAtIndex:indexPath.row];
//    ctr.historyPayModel = model;
//    [self.navigationController pushViewController:ctr animated:YES];
}

-(BOOL)hidesBottomBarWhenPushed
{
    return YES;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.textFiled resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
